package fi.foyt.cs.controller;

import java.util.Locale;

import fi.foyt.cs.persistence.dao.locale.LocaleElementDAO;
import fi.foyt.cs.persistence.dao.locale.LocaleTextDAO;
import fi.foyt.cs.persistence.domainmodel.locale.LocaleElement;
import fi.foyt.cs.persistence.domainmodel.locale.LocaleText;

public class LocaleController {
  
  public LocaleElement createLocaleElement() {
    LocaleElementDAO localeElementDAO = new LocaleElementDAO();
    return localeElementDAO.create();
  }
  
  public LocaleText findLocaleTextById(LocaleElement localeElement, Locale locale, String text) {
    LocaleTextDAO localeTextDAO = new LocaleTextDAO();
    return localeTextDAO.create(localeElement.getId().getId(), locale.getLanguage(), text);
  }
  
  public LocaleElement findLocaleElementById(Long localeElementId) {
    LocaleElementDAO localeElementDAO = new LocaleElementDAO();
    return localeElementDAO.findById(localeElementId);
  }
  
  public LocaleText findLocaleTextById(Long localeTextId) {
    LocaleTextDAO localeTextDAO = new LocaleTextDAO();
    return localeTextDAO.findById(localeTextId);
  }
  
  public LocaleText findLocaleTextByLocaleElementAndLocale(LocaleElement localeElement, Locale locale) {
    LocaleTextDAO localeTextDAO = new LocaleTextDAO();
    return localeTextDAO.findByLocaleElementIdAndLocale(localeElement.getId().getId(), locale.getLanguage());
  }
  
  public String getLocalizedText(LocaleElement localeElement, Locale locale) {
    LocaleText localeText = findLocaleTextByLocaleElementAndLocale(localeElement, locale);
    return localeText != null ? localeText.getText() : null;
  }
  
  public String getLocalizedText(Long localeElementId, Locale locale) {
    LocaleElement localeElement = findLocaleElementById(localeElementId);
    return getLocalizedText(localeElement, locale);
  }
  
  public LocaleText createLocaleText(LocaleElement localeElement, Locale locale, String text) {
    LocaleTextDAO localeTextDAO = new LocaleTextDAO();
    return localeTextDAO.create(localeElement.getId().getId(), locale.getLanguage(), text);
  }
  
  public LocaleText updateLocaleText(LocaleText localeText, String text) {
    LocaleTextDAO localeTextDAO = new LocaleTextDAO();
    return localeTextDAO.updateText(localeText, text);
  }
}
